import {message} from 'antd'
import {addCoupon, queryAdvertItem,editCoupon,removeCoupon, queryCoupon} from '../services/api';


export default {
  namespace: 'coupon',

  state: {
    data: {list: [], pagination: {size: 10, current: 1, total: 0}},
    item: {},
    couponAddModal: false,
    loading: false,
    confirmLoading: false,
  },

  effects: {
    * fetch({payload}, {call, put}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryCoupon, payload);
      yield put({
        type: 'save',
        payload: {pagination: response.page, list: response.data},
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    * fetchItem({payload, callback}, {call, put}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryAdvertItem, payload);
      yield put({
        type: 'saveItem',
        payload: response.data,
      });
      if (response.status && callback) {
        callback()
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    * remove({payload, callback}, {call, put}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(removeCoupon, payload);
      if (response.status) {
        message.success('删除成功');
        if (callback) {
          callback();
        }
      } else {
        message.success(response.message);
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    * add({payload, callback}, {call, put}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addCoupon, payload);
      if (response.status) {
        message.success('添加成功');
        if (callback) {
          callback();
        }
      } else {
        message.success(response.message);
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *status({payload, callback}, {call, put}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(editCoupon, payload);
      if (response.status) {
        message.success('编辑成功');
        if (callback) {
          callback();
        }
      } else {
        message.success(response.message);
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    // *fetchSalesData(_, { call, put }) {
    //   const response = yield call(fakeChartData);
    //   yield put({
    //     type: 'save',
    //     payload: {
    //       salesData: response.salesData,
    //     },
    //   });
    // },
  },

  reducers: {
    save(state, {payload}) {
      return {
        ...state,
        data: payload,
      };
    },
    saveItem(state, {payload}) {
      return {
        ...state,
        item: payload,
      };
    },
    changeVisible(state, {payload}) {
      return {
        ...state,
        couponAddModal: payload,
      };
    },
    changeLoading(state, {payload}) {
      return {
        ...state,
        loading: payload,
      };
    }
  },
};
